package com.dengdai.applibrary.activitys;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.dengdai.applibrary.BaseApplication;
import com.dengdai.applibrary.config.ResIdName;
import com.dengdai.applibrary.config.ViewInfo;
import com.dengdai.applibrary.interfaces.ActivityInterface;
import com.dengdai.applibrary.utils.BaseViewUtils;
import com.dengdai.applibrary.utils.OperationUtils;
import com.dengdai.applibrary.widgets.progres.CustomLoadingDialoge;
import com.ypy.eventbus.EventBus;

import butterknife.ButterKnife;

/**
 * @说明 基础Activity
 * @作者 LY
 * @时间 16/5/5 09:41
 * @邮箱 2743569843@qq.com
 * @版权 Copyright(c) 2016 LY-版权所有
 * @备注
 */
public abstract class BaseActivity extends FragmentActivity implements ActivityInterface {
    /**
     * 动画类型
     */
    private int mAnimationType = ANIMATION_NONE;
    /**
     * 业务操作
     */
    private OperationUtils mOperation = null;
    /**
     * 加载对话款
     */
    private CustomLoadingDialoge customLoadingDialoge;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResID());
        //初始化
        ButterKnife.bind(this);
        //把当前对象保存到数组中
        BaseApplication.addActivity(this);
        //注册消息监听
        EventBus.getDefault().register(this);
        //界面跳转业务处理
        mOperation = new OperationUtils(this);
        //初始化界面数据
        initData();
        //业务操作
        business();
    }


    @Override
    public void finish() {
        super.finish();
        switch (mAnimationType) {
            case ActivityInterface.ANIMATION_LEFT_RIGHT:
                overridePendingTransition(0, BaseViewUtils.gainResId(this, BaseViewUtils.ANIM, ViewInfo.SLIDE_RIGHT_OUT));
                break;
            case ActivityInterface.ANIMATION_TOP_BOTTOM:
                overridePendingTransition(0, BaseViewUtils.gainResId(this, BaseViewUtils.ANIM, ViewInfo.PUSH_UP_OUT));
                break;
            case ActivityInterface.ANIMATION_FADE_IN_OUT:
                overridePendingTransition(0, BaseViewUtils.gainResId(this, BaseViewUtils.ANIM, ViewInfo.FADE_OUT));
                break;
        }
        mAnimationType = ANIMATION_NONE;
    }

    /**
     * 获取界面跳转对象
     *
     * @return
     */
    protected OperationUtils getOperation() {
        if (mOperation == null) {
            mOperation = new OperationUtils(this);
        }
        return mOperation;
    }

    /**
     * 消息监听
     *
     * @param object 消息
     */
    protected void onEvent(Object object) {
        event(object);
    }

    @Override
    protected void onPause() {
        super.onPause();
        pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        resume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        destroy();
        //关闭消息监听
        EventBus.getDefault().unregister(this);
        //删除当前Activity
        BaseApplication.removeActivity(this);
    }

    @Override
    public void startLoadingDialoge() {
        startLoadingDialoge(getResources().getString(BaseViewUtils.gainResId(this, BaseViewUtils.STRING, ResIdName.LOADING)));
    }

    @Override
    public void startLoadingDialoge(String message) {
        if (customLoadingDialoge == null) {
            customLoadingDialoge = new CustomLoadingDialoge(this, message);
        }
        customLoadingDialoge.show();
    }

    @Override
    public void stopLoadingDialoge() {
        if (customLoadingDialoge != null) {
            customLoadingDialoge.dismiss();
            customLoadingDialoge = null;
        }
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void event(Object object) {

    }
}
